home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / DBMOPEN.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  5KB  |  160 lines

  1. /* dbmopen.c - Open the file for dbm operations. This looks like the
  2.    NDBM interface for dbm use. */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990  Free Software Foundation, Inc.
  6.  
  7.     GDBM is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     GDBM is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GDBM; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     You may contact the author by:
  22.        e-mail:  phil@wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.         phone:  (206) 676-3035
  28.        
  29. *************************************************************************/
  30.  
  31. /*
  32.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  33.  *
  34.  * To this port, the same copying conditions apply as to the
  35.  * original release.
  36.  *
  37.  * IMPORTANT:
  38.  * This file is not identical to the original GNU release!
  39.  * You should have received this code as patch to the official
  40.  * GNU release.
  41.  *
  42.  * MORE IMPORTANT:
  43.  * This port comes with ABSOLUTELY NO WARRANTY.
  44.  *
  45.  * $Header: e:/gnu/gdbm/RCS/dbmopen.c'v 1.4.0.1 90/08/16 09:22:12 tho Exp $
  46.  */
  47.  
  48. #include <stdio.h>
  49. #include <sys/types.h>
  50. #ifndef MSDOS
  51. #include <sys/file.h>
  52. #endif /* not MSDOS */
  53. #include <sys/stat.h>
  54. #include "gdbmdefs.h"
  55. #include "systems.h"
  56. #include "gdbmerrno.h"
  57. #include "extern.h"
  58.  
  59. extern gdbm_error gdbm_errno;
  60.  
  61. /* Initialize ndbm system.  FILE is a pointer to the file name.  In
  62.    standard dbm, the database is found in files called FILE.pag and
  63.    FILE.dir.  To make gdbm compatable with dbm using the dbminit call,
  64.    the same file names are used.  Specifically, dbminit will use the file
  65.    name FILE.pag in its call to gdbm open.  If the file (FILE.pag) has a
  66.    size of zero bytes, a file initialization procedure is performed,
  67.    setting up the initial structure in the file.  Any error detected will
  68.    cause a return value of -1.  No errors cause the return value of 0.
  69.    NOTE: file.dir will be ignored and will always have a size of zero.
  70.    FLAGS and MODE are the same as the open (2) call.  This call will
  71.    look at the FLAGS and decide what call to make to gdbm_open.  For
  72.    FLAGS == O_RDONLY, it will be a GDBM_READER, if FLAGS == O_RDWR|O_CREAT,
  73.    it will be a GDBM_WRCREAT (creater and writer) and if the FLAGS == O_RDWR,
  74.    it will be a GDBM_WRITER and if FLAGS contain O_TRUNC then it will be
  75.    a GDBM_NEWDB.  All other values of FLAGS in the flags are
  76.    ignored. */
  77.  
  78. gdbm_file_info *
  79. dbm_open (file, flags, mode)
  80.      char *file;
  81.      int flags;
  82.      int mode;
  83. {
  84.   char* pag_file;        /* Used to construct "file.pag". */
  85.   char* dir_file;        /* Used to construct "file.dir". */
  86.   struct stat dir_stat;        /* Stat information for "file.dir". */
  87.   gdbm_file_info *temp_dbf;  /* Temporary file pointer storage. */
  88.  
  89.  
  90.   /* Prepare the correct names of "file.pag" and "file.dir". */
  91.   pag_file = (char *) alloca (strlen (file)+5);
  92.   dir_file = (char *) alloca (strlen (file)+5);
  93.  
  94.   strcpy (pag_file, file);
  95.   strcat (pag_file, ".pag");
  96.   strcpy (dir_file, file);
  97.   strcat (dir_file, ".dir");
  98.   
  99.  
  100.   /* Call the actual routine, saving the pointer to the file information. */
  101.   flags &= O_RDONLY | O_RDWR | O_CREAT | O_TRUNC;
  102.   if (flags == O_RDONLY)
  103.     {
  104.       temp_dbf = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL);
  105.     }
  106.   else if (flags == (O_RDWR | O_CREAT))
  107.     {
  108.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRCREAT, mode, NULL);
  109.     }
  110.   else if ( (flags & O_TRUNC) == O_TRUNC)
  111.     {
  112.       temp_dbf = gdbm_open (pag_file, 0, GDBM_NEWDB, mode, NULL);
  113.     }
  114.   else
  115.     {
  116.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL);
  117.     }
  118.  
  119.   /* Did we successfully open the file? */
  120.   if (temp_dbf == NULL)
  121.     {
  122.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  123.       return NULL;
  124.     }
  125.  
  126.   /* If the database is new, link "file.dir" to "file.pag". This is done
  127.      so the time stamp on both files is the same. */
  128.   if (stat (dir_file, &dir_stat) == 0)
  129.     {
  130.       if (dir_stat.st_size == 0)
  131. #ifdef MSDOS
  132.         if (open (dir_file, O_RDWR|O_TRUNC, S_IREAD|S_IWRITE) < 0)
  133. #else /* not MSDOS */
  134.     if (unlink (dir_file) != 0 || link (pag_file, dir_file) != 0)
  135. #endif /* not MSDOS */
  136.       {
  137.         gdbm_errno = GDBM_FILE_OPEN_ERROR;
  138.         gdbm_close (temp_dbf);
  139.         return NULL;
  140.       }
  141.     }
  142.   else
  143.     {
  144.       /* Since we can't stat it, we assume it is not there and try
  145.          to link the dir_file to the pag_file. */
  146. #ifdef MSDOS
  147.       if (open (dir_file, O_RDWR|O_CREAT, S_IREAD|S_IWRITE) < 0)
  148. #else /* not MSDOS */
  149.       if (link (pag_file, dir_file) != 0)
  150. #endif /* not MSDOS */
  151.     {
  152.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  153.       gdbm_close (temp_dbf);
  154.       return NULL;
  155.     }
  156.     }
  157.  
  158.   return temp_dbf;
  159. }
  160.